<?php echo view('header.html');?>

<body class="layui-view-bodys">
    <div class="layui-form-iframe">
        <div class="layui-form mokui-form layui-card-body">
            <div class="layui-form">
                <div class="layui-form-item">
                    <label class="layui-form-label">
                        上级/顶级：
                    </label>
                    <div class="layui-input-inline">
                        <select name="data[pid]">
                            <option value=""> 作为顶级</option>
                            <?php if($parent):?>
                            <?php foreach($parent as $v):?>
                            <option value="<?=$v['id']?>" <?php if($pid==$v['id']):?>selected<?php endif?>><?=$v['spacer']?><?=$v['name']?></option>
                            <?php endforeach?>
                            <?php endif?>
                        </select>
                    </div>
                </div>
                <div class="multies">
                    <?php if(isset($son) && $son):?>
                    <?php foreach($son as $k=>$v):?>
                    <div class="layui-form-item">
                        <div class="layui-inline"><label class="layui-form-label">(<?=$k+1?>):</label>
                            <div class="layui-input-inline mokui-md1"><input autocomplete="off" class="layui-input layui-disabled" disabled readonly placeholder="名称" type="text" value="<?=$v['name']?>" /></div>
                            <div class="layui-input-inline mokui-md2"><input autocomplete="off" class="layui-input layui-disabled" disabled readonly placeholder="URI" type="text" value="<?=$v['uri']?>" /></div>
                            <div class="layui-input-inline"><input type="checkbox" class="layui-disabled" value="1" <?=$v['skip']==1 ? 'checked' :''?> title="不用验证" lay-skin="primary"></div>
                        </div>
                    </div>
                    <?php endforeach;?>
                    <?php else:?>
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">(1):</label>
                            <div class="layui-input-inline mokui-md1">
                                <input autocomplete="off" class="layui-input" lay-verify="required" name="data[0][name]" placeholder="名称" type="text" value="<?=isset($data[0]['name']) ? $data[0]['name'] : ''?>" />
                            </div>
                            <div class="layui-input-inline mokui-md2">
                                <input autocomplete="off" class="layui-input" name="data[0][uri]" placeholder="URI" type="text" value="<?=isset($data[0]['uri']) ? $data[0]['uri'] : ''?>" />
                            </div>
                            <div class="layui-input-inline">
                                <input type="checkbox" name="data[0][skip]" value="1" <?php if(isset($data[0]['skip']) && $data[0]['skip']=='1'):?>checked<?php endif?> title="不用验证" lay-skin="primary">
                            </div>
                        </div>
                    </div>
                    <?php endif;?>

                </div>
                <?php if(!isset($id) || !$id):?>
                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button class="layui-btn layui-btn-sm layui-btn-default" id="add_group">添加一组</button>
                    </div>
                </div>
                <?php endif?>

                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button class="layui-btn layui-btn-blue" lay-filter="ams_submit" lay-submit="">提交保存</button>
                    </div>
                </div>

            </div>
        </div>
    </div>
    <?php echo view('footer.html');?>
    <script type="text/javascript">
        layui.use(['layer', 'jquery', 'form'], function () {
            var layer = layui.layer,
                $ = layui.jquery,
                form = layui.form;
            

            form.on('submit(ams_submit)', function (data) {

                var url = window.location.href;
                $.post(url, data.field, function (res) {
                    if (res.code == 0) {
                        layer.msg(res.msg, { icon: 1, time: 1000 }, function () {
                            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                            parent.location.href = adminpath + 'admin/rbac/rule';
                            parent.layer.close(index);
                        });
                    } else {
                        layer.msg(res.msg, { icon: 5, time: 1500, anim: 6 });
                    }
                }, 'json');
                return false;
            });

            //添加一组
            $("#add_group").on('click', function () {
                var k, i = $(".multies .layui-form-item").length;
                k = i;
                i = i + 1;
                var str = '<div class="layui-form-item"><div class="layui-inline"><label class="layui-form-label">(' + i + '):</label>';
                str += '<div class="layui-input-inline mokui-md1"><input autocomplete="off" class="layui-input" name="data[' + k + '][name]" placeholder="名称" type="text" value="" /></div>';
                str += '<div class="layui-input-inline mokui-md2"><input autocomplete="off" class="layui-input" name="data[' + k + '][uri]" placeholder="URI" type="text" value="" /></div>';
                str += '<div class="layui-input-inline"><input type="checkbox" name="data[' + k + '][skip]" value="1" title="不用验证" lay-skin="primary"></div></div></div>';
                // str += '</div></div>';
                $('.multies').append(str);
                form.render();
            })
        })
    </script>
</body>
